﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Xml.Serialization;

namespace Alogient.Cameleon.Community.Blog.Items
{
    [Serializable]
    [XmlRoot("Article")]
    [XmlType("Article")]
    public class CMArticle : IAlerte
    {
        #region Fields

        private List<CMComment> comments;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        internal CMArticle()
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Get or Set Parent Blog Id
        /// </summary>
        [XmlAttribute]
        public int BlogID { get; set; }

        /// <summary>
        /// Get or Set article Id
        /// </summary>
        [XmlAttribute]
        public int ArticleID { get; set; }

        /// <summary>
        /// FGet or Set the culture Code
        /// </summary>
        [XmlAttribute]
        public string CultureCode { get; set; }

        /// <summary>
        /// Get or Set the creation date
        /// </summary>
        [XmlAttribute]
        public DateTime CreationDate { get; set; }

        /// <summary>
        /// Get or Set the last modified date
        /// </summary>
        [XmlAttribute]
        public DateTime LastModified { get; set; }

        /// <summary>
        /// Get or Set title
        /// </summary>
        [XmlAttribute]
        public string Title { get; set; }

        /// <summary>
        /// Get or Set title
        /// </summary>
        [XmlAttribute]
        public string Teaser { get; set; }

        /// <summary>
        /// Get or Set title
        /// </summary>
        [XmlAttribute]
        public string Author { get; set; }

        /// <summary>
        /// Get or Set the article's text
        /// </summary>
        [XmlAttribute]
        public string Text { get; set; }

        /// <summary>
        /// List of comments or replies that have been made to an article
        /// </summary>
        [XmlAttribute]
        public List<CMComment> Comments
        {
            get
            {
                if (this.comments == null)
                {
                    this.comments = this.GetComments();
                }

                return this.comments;
            }
        }

        #endregion


        #region Methods

        public List<MembershipUser> GetSuscribers()
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        [Obsolete("Reloads the User provided as a parameter", true)]
        public MembershipUser GetSuscriber(MembershipUser suscriber)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        public bool UserIsSubscribed(MembershipUser user)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        public List<MembershipUser> AddSuscriber(MembershipUser suscriber)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        public List<MembershipUser> RemoveSuscriber(MembershipUser suscriber)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }
        
        /// <summary>
        /// Add a comment to the article
        /// </summary>
        /// <param name="title">title of the comment</param>
        /// <param name="text">comment to add</param>
        /// <returns>The list of comment updated</returns>
        public CMComment AddComment(string title, string text, Guid authorId, string authorFirstName, string authorLastName)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        /// <summary>
        /// Remove a comment for the article
        /// </summary>
        /// <param name="commentID">comment to remove</param>
        /// <returns>The list of comment updated</returns>
        public List<CMComment> RemoveComment(int commentID)
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }


        /// <summary>
        /// Save the Article to the database
        /// </summary>
        public bool Save()
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        public CMTag[] GetTags()
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        public bool SetTags(IList<string> tags)
        {
            List<CMTag> currentCMTags = new List<CMTag>(this.GetTags());

            List<string> currentTags = new List<string>();

            foreach (CMTag tag in currentCMTags)
            {
                if (!tags.Contains(tag.Name) || tag.Name.Equals(string.Empty))
                {
                    // Delete Article tag
                    throw new NotImplementedException("Not implemented"); // TODO
                }
                else
                {
                    currentTags.Add(tag.Name);
                }
            }

            foreach (string tag in tags)
            {
                string t = tag.Trim();
                if (!currentTags.Contains(t) && !t.Equals(string.Empty))
                {
                    // Add article tag
                    throw new NotImplementedException("Not implemented"); // TODO
                }
            }

            return true;
        }

        /// <summary>
        /// Get comments of this article
        /// </summary>
        /// <returns>List of comments link to an article</returns>
        internal List<CMComment> GetComments()
        {
            throw new NotImplementedException("Not implemented"); // TODO
        }

        #endregion Methods
    }
}